﻿using System.Collections.Generic;
using System.Threading.Tasks;
using WeiboWPSdk.Json;
using WeiboWPSdk.Json.Linq;
using WeiboWPSdk.ResponseEntity;

namespace WeiboWPSdk.Api {
    public class UserApi : ApiBase {
        public UserApi(ClientHttpHelper httpHelper)
            :base(httpHelper) {
    }

        #region 读取接口
        /// <summary>
        /// 根据用户ID获取用户信息
        /// http://open.weibo.com/wiki/2/users/show
        /// 通过个性化域名获取用户资料以及用户最新的一条微博
        /// http://open.weibo.com/wiki/2/users/domain_show
        /// 三个参数只能选择其中一项，其他设为 null
        /// </summary>
        /// <param name="uid">需要查询的用户ID。</param>
        /// <param name="screenName">需要查询的用户显示名</param>
        /// <param name="domain">需要查询的个性化域名</param>
        /// <returns></returns>
        public async Task<User> ShowAsync(long? uid = null, string screenName = null, string domain = null) {
            string json = string.Empty;
            if (domain == null)
                json = await HttpHelper.GetBasicCommandAsync("users/show", new WeiboParameter("uid", uid), new WeiboParameter("screen_name", screenName));
            else
                json = await HttpHelper.GetBasicCommandAsync("users/domain_show", new WeiboParameter("domain", domain));

            return JsonConvert.DeserializeObject<User>(json);
        }
        /// <summary>
        /// 批量获取用户的粉丝数、关注数、微博数
        /// http://open.weibo.com/wiki/2/users/counts
        /// </summary>
        /// <param name="uids">需要获取数据的用户UID，多个之间用逗号分隔，最多不超过100个。</param>
        /// <returns></returns>
        public async Task<IEnumerable<UserCounts>> GetCountsAsync(params long[] uids) {
            var json = await HttpHelper.GetBasicCommandAsync("users/counts", new WeiboParameter("uids", string.Join(",", uids)));
            return JsonConvert.DeserializeObject<IEnumerable<UserCounts>>(json);
        }
        /// <summary>
        /// 获取用户等级信息
        /// http://open.weibo.com/wiki/2/users/show_rank
        /// </summary>
        /// <param name="uid">需要查询的用户ID。</param>
        /// <returns></returns>
        public async Task<int> ShowRankAsync(long uid) {
            var json = await HttpHelper.GetBasicCommandAsync("users/show_rank", new WeiboParameter("uid", uid));
            return (int)JObject.Parse(json)["rank"];
        }
        #endregion
    }
}
